package com.eascs.admin.o2o.oaotodata.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.loan.service.O2oSdService;
import com.eascs.admin.o2o.oaotodata.service.OaO2oDataService;
import com.eascs.admin.o2o.oaotodata.trans.Constant4Ospm;
import com.eascs.admin.o2o.oaotodata.trans.SDElement;
import com.eascs.admin.o2o.zx.entity.O2oLoanUserEntityZx;
import com.eascs.admin.o2o.zx.vo.O2oZxUserVo;
import com.eascs.admin.oldO2olike.form.OldO2oLikeForm;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.crm.nsEditDetail.entity.EadbEabaCustomerNew;
import com.eascs.site.view.ViewContext;

import net.sf.json.JSONObject;

/**
* @author bao.pan 2017年7月15日 下午2:32:06
*/
@RestController
@RequestMapping("/oaoro2odatasub")
public class JsonAjaxController {
	private final Logger logger = LoggerFactory.getLogger(JsonAjaxController.class);
	@Value("${sd.url}")
	private String sdUrl;
	@Autowired
	OaO2oDataService oaO2oDataService;
	@Autowired
	private O2oSdService o2oSdService;
	
	@RequestMapping(value="/jsonword",method=RequestMethod.GET,produces = "text/html;charset=UTF-8")
	public String findid(String reloadname) throws Exception{
		List<SDElement> sds=new ArrayList<SDElement>();
		JSONObject datas = new JSONObject();
		if (!StringUtils.isBlank(reloadname)||reloadname.length()<4) {
			datas.put(Constant4Ospm.TOTAL, sds.size());
			datas.put(Constant4Ospm.PAGE_SIZE, sds);
			return datas.toString();
		}
		String data = "<requestxml><name>" + reloadname + "</name>" + "</requestxml>";
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(sdUrl);
			call.setOperationName("getSDCustomerO2OData");
			call.setTimeout(20000);
			String s = (String) call.invoke(new String[] { data });
			// System.out.println(System.currentTimeMillis() - beginTime);
			Document doc = DocumentHelper.parseText(s);
			Element root = doc.getRootElement().element("sdcustomerdata");
			if (root != null) {
				@SuppressWarnings("unchecked")
				List<Element> list = root.elements();
				Element e  = null;
				for (int i=0;i<list.size();i++) {
					 e  = list.get(i);
					 SDElement sdElement=new SDElement();
					String cno = String.valueOf(e.element("cno").getData() == null ?0l: e.element("cno").getData());
					String ccode = String.valueOf(e.element("ccode").getData() == null ?0l: e.element("ccode").getData());
					String cname = String.valueOf(e.element("cname").getData() == null ?0l: e.element("cname").getData());
					String cnames = String.valueOf(e.element("cnames").getData() == null ?0l: e.element("cnames").getData());
					sdElement.setCno(cno);
					sdElement.setCcode(ccode);
					sdElement.setCname(cname);
					sdElement.setCnames(cnames);
					sds.add(sdElement);
				}
			}

		} catch (Exception e) {
			ViewContext.put("sdInfo", "fail");
			logger.error("拉取sd数据出错，错误信息为:", e);
			//return BaseRestResult.error("拉取sd数据出错:");
//			BaseRestResult<List<Object[]>> s = BaseRestResult.buildRestResult(listData);
//			s.setAction(BaseRestResult.ACTION_ERROR);
		}
		// 设置总共有多少条记录
		datas.put(Constant4Ospm.TOTAL, sds.size());
		// 设置当前页的数据
		datas.put(Constant4Ospm.PAGE_SIZE, sds);
		
		return datas.toString();
	}
	
	@RequestMapping(value="/jsonwordNew",method=RequestMethod.GET,produces = "text/html;charset=UTF-8")
	public String findNewid(String reloadname) throws Exception{
		List<EadbEabaCustomerNew> sds=new ArrayList<EadbEabaCustomerNew>();
		List<SDElement> sdsnew=new ArrayList<SDElement>();
		JSONObject datas = new JSONObject();
		sds = o2oSdService.getCustomerDataSouce1(reloadname);
		
		
		for(int i =0;i<sds.size();i++){
			EadbEabaCustomerNew eadbEabaCustomer = new EadbEabaCustomerNew();
			eadbEabaCustomer = sds.get(i);
			SDElement sdElement=new SDElement();
			sdElement.setCno(eadbEabaCustomer.getcNo());
			sdElement.setCcode(eadbEabaCustomer.getcCode());
			sdElement.setCname(eadbEabaCustomer.getcName());
			sdElement.setCnames(eadbEabaCustomer.getcNames());
			sdsnew.add(sdElement);
		}
		// 设置总共有多少条记录
		datas.put(Constant4Ospm.TOTAL, sdsnew.size());
		// 设置当前页的数据
		datas.put(Constant4Ospm.PAGE_SIZE, sdsnew);
				return datas.toString();
	}
	@RequestMapping(value="/jsonwordzx",method=RequestMethod.POST,produces = "text/html;charset=UTF-8")
	public String findo2ozx(String reloadname,String rows,String page) throws Exception{
		OldO2oLikeForm oldO2oLikeForm=new OldO2oLikeForm();
		oldO2oLikeForm.setBrandName(reloadname);
		oldO2oLikeForm.setPageIndex(Integer.valueOf(page)-1);
		JSONObject datas = new JSONObject();
		oldO2oLikeForm.setPageSize(Integer.valueOf(rows));
		PageResult<O2oZxUserVo> pageResult=oaO2oDataService.findZXByPage(oldO2oLikeForm);	
		// 设置总共有多少条记录
		datas.put(Constant4Ospm.TOTAL, pageResult.getDbRowCount());
		// 设置当前页的数据
		datas.put(Constant4Ospm.PAGE_SIZE, pageResult.getData());
		return datas.toString();
	}
}
